<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>Show</title>
</head>

<body>
    <script type="text/javascript">
        function do_del(id) {
            var is = window.confirm("确定要删除这条留言吗? ");
            if (is) {
                window.location.href = "delete.php?id=" + id;
            }
        }
    </script>

    <?php
    session_start();
    if (!(isset($_SESSION['isLogged']) && $_SESSION['isLogged'])) {
        echo '未登录!'.' <a href="login.php">点我登录</a>';
        die();
    }
    include "index.html";
    include "conn.php";

    $searchSQL = "";

    $isCid = !empty($_GET['category']);
    $c_id = $isCid ? $_GET['category'] : -1;
    $c_get = "category=" . $c_id;
    $category = $conn->query("select * from category");
    ?>
    <a href="show.php">主页|</a>
    <?php
    while ($row = $category->fetch_array()) { ?>
        <a href="show.php?category=<?php echo $row['c_id'] ?>"><?php echo $row['c_name']; ?>|</a>
    <?php
    }
    $searchSQL = $isCid ? " and message.c_id={$c_id} " : "";

    //搜索功能
    $isSearch = !empty($_GET['key']);
    $key = $isSearch ? $_GET['key'] : "";
    if ($isSearch) {
        $searchSQL = $isCid ? $searchSQL . " and title like '%$key%' " : " and title like '%$key%' ";
    }
    $keyGet = "key=" . $key;


    //分页功能
    $pageSize = 10; //每页条数
    $count = $conn->query("select count(*) num from `message` where 1 " . $searchSQL);
    $num = $count->fetch_array()['num']; //总记录条数
    $page = empty($_GET['page']) ? 1 : $_GET['page']; //当前页码
    $lastPage = ceil($num / $pageSize); //尾页页码
    $offset = ($page - 1) * $pageSize;

    $sql = "select * from `message`,`category` where message.c_id=category.c_id " . $searchSQL . " order by id" . " limit $offset, $pageSize";
    $result = $conn->query($sql);
    while ($row = $result->fetch_array()) { ?>
        <p><?php echo "[" . $row['c_name'] . "]" ?><a href="detail.php?id=<?php echo $row['id'] ?>"><?php echo $row['title']; ?></a></p>
    <?php
    }
    ?>
    <?php
    if ($page != 1) {
    ?>
        <a href="show.php<?php if ($isSearch) {
                                echo "?" . $keyGet;
                            }
                            if ($isCid) {
                                echo "&" . $c_get;
                            } ?>">首页</a> |
    <?php
    }
    ?>
    <?php
    if ($page == 1) {
        echo "上一页";
    } else { ?>
        <a href="?page=<?php echo $page - 1;
                        if ($isSearch) {
                            echo "&" . $keyGet;
                        }
                        if ($isCid) {
                            echo "&" . $c_get;
                        } ?>">上一页</a>
    <?php } ?> |
    <?php
    $lim = $lastPage > 5 ? 5 : $lastPage;
    $l = $page - floor($lim / 2);
    $h = $page + floor($lim / 2);
    if ($l < 1) {
        $l = 1;
        $h = $l + ($lim - 1);
    } else if ($h > $lastPage) {
        $h = $lastPage;
        $l = $h - ($lim - 1);
    }
    for ($i = $l; $i <= $h; $i = $i + 1) {
        if ($i == $page) {
            echo $i;
        } else { ?>
            <a href="?page=<?php echo $i;
                            if ($isSearch) {
                                echo "&" . $keyGet;
                            }
                            if ($isCid) {
                                echo "&" . $c_get;
                            } ?>"><?php echo $i; ?></a>
        <?php  } ?>
    <?php
    }
    ?> |
    <?php
    if ($page == $lastPage) {
        echo "下一页";
    } else { ?>
        <a href="?page=<?php echo $page + 1;
                        if ($isSearch) {
                            echo "&" . $keyGet;
                        }
                        if ($isCid) {
                            echo "&" . $c_get;
                        }
                        ?>">下一页|</a>
    <?php } ?>
    <?php
    if ($page != $lastPage) {
    ?>
        <a href="?page=<?php echo $lastPage;
                        if ($isSearch) {
                            echo "&" . $keyGet;
                        }
                        if ($isCid) {
                            echo "&" . $c_get;
                        } ?>">尾页</a>
    <?php
    }
    ?>
</body>

</html>